package com.pickgliss.ui.controls.container
{
	import com.pickgliss.effect.IEffect;
	import com.pickgliss.events.ComponentEvent;
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.ui.core.Disposeable;
	import com.pickgliss.utils.ObjectUtils;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	/**
	 * 
	 * @author Herman
	 * 一个支持简单排列的容器
	 * 
	 */	
	public class BoxContainer extends Component
	{
		public static const P_childRefresh:String="childRefresh";
		public static const P_childResize:String = "childResize";
		public static const P_isReverAdd:String = "isReverAdd";
		public static const P_spacing:String = "spacing";
		
		public static const P_strictWidth:String = "strictWidth";
		public static const P_strictHeight:String = "strictHeight";
		
		public static const P_direction:String = "direction";
		
		public function BoxContainer()
		{
			_childrenList = new Vector.<DisplayObject>();
			super();
		}

		protected var _childrenList:Vector.<DisplayObject>;
		protected var _isReverAdd:Boolean;
		protected var _spacing:Number = 0;
		
		protected var _strictWidth:Number = -1;
		protected var _strictHeight:Number = -1;
		protected var _direction:Number = -1;

		override public function addChild(child:DisplayObject):DisplayObject
		{
			if(_childrenList.indexOf(child)>=0)return null;
			if(!_isReverAdd)
			{
				_childrenList.push(super.addChild(child));
			}else
			{
				_childrenList.unshift(super.addChildAt(child,0));
			}
			child.addEventListener(ComponentEvent.PROPERTIES_CHANGED, __onResize);
			onPropertiesChanged(P_childRefresh);
			return child;
		}
		override public function addChildAt(child:DisplayObject, index:int):DisplayObject{
			_childrenList.splice(index, 0, super.addChildAt(child, index));
			child.addEventListener(ComponentEvent.PROPERTIES_CHANGED, __onResize);
			onPropertiesChanged(P_childRefresh);
			return child;
		}
		override public function dispose():void
		{
			super.dispose();
			_childrenList = null;
		}
		public function removeAllChild():void
		{
			while(numChildren > 0)
			{
				var child:DisplayObject = getChildAt(0);
				removeChild(child);
				child.x = 0;
				child.removeEventListener(ComponentEvent.PROPERTIES_CHANGED, __onResize);
			}
			if(_childrenList)_childrenList.splice(0,-1);
		}
		
		protected function refreshDirection(child:DisplayObject):void
		{
			
		}
		/**
		 * 释放所有子对象 
		 * 
		 */		
		public function disposeAllChildren():void
		{
			ObjectUtils.disposeAllChildren(this);
		}
		public function set isReverAdd(value:Boolean):void
		{
			if(_isReverAdd == value)return;
			_isReverAdd = value;
			onPropertiesChanged(P_isReverAdd);
		}
		/**
		 * 更新位置
		 * 
		 */
		public function refreshChildPos():void
		{
			onPropertiesChanged(P_childResize);
		}
		
		override public function removeChild(child:DisplayObject):DisplayObject
		{
			child.removeEventListener(ComponentEvent.PROPERTIES_CHANGED, __onResize);
			_childrenList.splice(_childrenList.indexOf(child),1);
			super.removeChild(child);
			onPropertiesChanged(P_childRefresh);
			return child;
		}
		
		override public function removeChildAt(index:int):DisplayObject
		{
			var child:DisplayObject = getChildAt(index);
			child.removeEventListener(ComponentEvent.PROPERTIES_CHANGED, __onResize);
			_childrenList.splice(_childrenList.indexOf(child),1);
			super.removeChild(child);
			onPropertiesChanged(P_childRefresh);
			return child;
		}
		
		public function reverChildren():void
		{
			var tempAllChildren:Vector.<DisplayObject> = new Vector.<DisplayObject>();
			while (numChildren>0)
			{
				tempAllChildren.push(removeChildAt(0));
			}
			for (var i:int = 0; i<tempAllChildren.length; i++)
			{
				addChild(tempAllChildren[i]);
			}
		}
		
		public function get spacing():Number
		{
			return _spacing;
		}

		/**
		 * 
		 * @param value 间距
		 * 当是VBox对象时此值表示的是纵向的间距
		 * 当是HBox对象时此值表示的是横向的间距
		 * 
		 */			
		public function set spacing(value:Number):void
		{
			if(_spacing == value) return;
			_spacing = value;
			onPropertiesChanged(P_spacing);
		}
		
		public function set strictWidth(value:Number):void
		{
			if(_strictWidth == value)return;
			_strictWidth = value;
			onPropertiesChanged(P_strictWidth);
		}
		
		public function set strictHeight(value:Number):void
		{
			if(_strictHeight == value)return;
			_strictHeight = value;
			onPropertiesChanged(P_strictHeight);
		}
		
		public function set direction(value:Number):void
		{
			if(_direction == value)return;
			_direction = value;
			onPropertiesChanged(P_direction);
		}
		/**
		 *执行排列的方法 
		 * 
		 */			
		protected function arrange():void
		{

		}
		
		protected function get isStrictWidth():Boolean
		{
			return _strictWidth > 0;
		}
		
		protected function get isStrictHeight():Boolean
		{
			return _strictHeight > 0;
		}

		override protected function onProppertiesUpdate():void
		{
			arrange();
		}
				
		protected function __onResize(event:ComponentEvent):void
		{
			if (event.changedProperties[Component.P_height] || event.changedProperties[Component.P_width])
			{
				onPropertiesChanged(P_childRefresh);
			}
		}
	}
}